package com.jshongli.myapplication.common.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.jshongli.myapplication.R;



public class SuccessDialogUtils extends Dialog {
    private static final String TAG = "LoadingDialog";

    private Context mContext;
    private String mMessage;
    private int mImageId;
    private boolean mCancelable;
    TextView tv_loading;
    ImageView iv_loading;
//    private RotateAnimation mRotateAnimation;

    public SuccessDialogUtils(@NonNull Context context, String message, int imageId) {
//        this(context,R.style.LoadingDialog,message,imageId,false);
        super(context, imageId);
        this.mMessage = message;
        this.mImageId = imageId;
        this.mContext = context;
    }

    public SuccessDialogUtils(@NonNull Context context, int themeResId, String message, int imageId, boolean cancelable) {
        super(context, themeResId);
        this.mMessage = message;
        this.mImageId = imageId;
        this.mCancelable = cancelable;
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        initView();
    }

    private void initView() {

        setContentView(R.layout.dialog_success);
        // 设置窗口大小
        WindowManager windowManager = getWindow().getWindowManager();
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.7f;
//        attributes.width = screenWidth/3;
//        attributes.height = attributes.width/2;
        getWindow().setAttributes(attributes);
        setCancelable(mCancelable);

        tv_loading = findViewById(R.id.tv_loading);
        iv_loading = findViewById(R.id.iv_loading);
        tv_loading.setText(mMessage);
        iv_loading.setImageResource(mImageId);
        iv_loading.measure(0,0);
//        mRotateAnimation = new RotateAnimation(0,360,iv_loading.getMeasuredWidth()/2,iv_loading.getMeasuredHeight()/2);
//        mRotateAnimation.setInterpolator(new LinearInterpolator());
//        mRotateAnimation.setDuration(1000);
//        mRotateAnimation.setRepeatCount(-1);
//        iv_loading.startAnimation(mRotateAnimation);
    }

    public void setImage(String message, int imageId){
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_success, null);
        tv_loading =view.findViewById(R.id.tv_loading);
        iv_loading = view.findViewById(R.id.iv_loading);
        tv_loading.setText(message);
        iv_loading.setImageResource(imageId);
    }


    @Override
    public void dismiss() {
//        mRotateAnimation.cancel();
        super.dismiss();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            // 屏蔽返回键
            return mCancelable;
        }
        return super.onKeyDown(keyCode, event);
    }
}
